import { queryRoleList, addRole,deleteRole,updateRole,queryTreeOrgList, addUserRole, queryMenuTree, addMenuRight,queryListForRoleId } from '@/services/system/sysRole';
import {queryUserList,} from '@/services/system/sysUser';
export default {
  namespace: 'role',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },

    queryForm:{},

  },

  effects: {
    *queryList({ payload }, { call, put }) {
      const response = yield call(queryRoleList, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addRole, payload);
      yield put({
        type: 'saveAdd',
        payload: response,
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(deleteRole, payload);
      yield put({
        type: 'saveRemove',
        payload: response,
      });
      if (callback) callback(response);
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateRole, payload);
      yield put({
        type: 'saveUpdate',
        payload: response,
      });
      if (callback) callback(response);
    },
    * getTree({payload,callback}, {call, put}) {
      const response = yield call(queryTreeOrgList, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
      if (callback) callback(response);
    },
    * getList({payload, callback}, {call, put}) {
      const response = yield call(queryUserList, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback(response);
    },
    * bindUserRole({payload, callback}, {call, put}) {
      const response = yield call(addUserRole, payload);
      yield put({
        type: 'saveBind',
        payload: response,
      });
      if (callback) callback(response);
    },
    * menuTree({payload, callback}, {call, put}) {
      const response = yield call(queryMenuTree, payload);
      yield put({
        type: 'savemenuTree',
        payload: response,
      });
      if (callback) callback(response);
    },

    * menuRight({payload, callback}, {call, put}) {
      const response = yield call(addMenuRight, payload);
      yield put({
        type: 'savemenuRight',
        payload: response,
      });
      if (callback) callback(response);
    },
    * listForRoleId({payload, callback}, {call, put}) {
      const response = yield call(queryListForRoleId, payload);
      yield put({
        type: 'savelistForRoleId',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
  },
};
